package UILayer;
import ControlLayer.PersonController;
import ModelLayer.*;
import java.util.Scanner;

/**
 * Write a description of class PersonUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonUI
{
    // instance variables - replace the example below with your own
    private PersonController personController;
    private Scanner reader;

    /**
     * Constructor for objects of class PersonUI
     */
    public PersonUI()
    {
        // initialise instance variables
        personController = new PersonController();
        reader = new Scanner(System.in);
        personMenu();
    }

    
    private void personMenu()
    {
        boolean exit = false;
                while(!exit)
        {
            int choice = printPersonMenu();
            if (choice == 1)
            {
               createPerson();
            }
            else
            {
                if(choice == 2)
                {
                    findPerson();
                }
                else
                {
                    if(choice == 3)
                    {
                        updatePerson();
                    }
                    else
                    {
                        if(choice == 4)
                        {
                            deletePerson();
                        }
                        else
                        {
                            exit = true;
                        }
                    }
                }
            }
        }
    
    }
    private int printPersonMenu()
    {       
        System.out.println("\f*** Person Menu *** ");
        System.out.println(" (1) Create Person ");
        System.out.println(" (2) Find and show Person ");
        System.out.println(" (3) Update Person ");
        System.out.println(" (4) Delete Person ");
        System.out.println("");
        System.out.println(" (0) Return to MainMenu ");
        System.out.print("\n Make your choice: ");
        int choice = UIInput.inputInt();
        return choice;
    }
    
    private int getValidRole()
    {
        System.out.println(" *** What type of Person? *** ");
        System.out.println(" (1) Private Customer ");
        System.out.println(" (2) Business Customer ");
        System.out.println(" (3) SalesMan ");
        System.out.println(" (4) Manager ");
        System.out.println(" (5) Warehouse Employee ");      
        System.out.print("\n Make your choice: ");
        int choice = UIInput.inputInt();
        return choice;   
    }
    
    private String selectPersonRole(int choice)
    {
        int newChoice = choice;
        String newType = null;
        switch(choice)
        {
            case 1: newType = "PrivateCustomer";    break;
            case 2: newType = "BusinessCustomer";   break;
            case 3: newType = "SalesMan";           break;
            case 4: newType = "Manager";            break;
            case 5: newType = "WarehouseEmployee";  break;
        }
        return newType;
    }

    private void createPerson()
    {
    System.out.println("\f");  
    String newRole = selectPersonRole(getValidRole());
    computeNeededInfo(newRole);
    }
    
    
    private void computeNeededInfo(String newRole)
    {
        if(newRole == null)
        
        {
            back();
        }
        else{
            if(newRole.equals("PrivateCustomer"))
            {
                createPrivateCustomer();
            }
            if(newRole.equals("BusinessCustomer"))
            {
                createBusinessCustomer();
            }
            if(newRole.equals("SalesMan"))
            {
                createSalesman();
            }
            if(newRole.equals("Manager"))
            {
                createManager();
            }
            if(newRole.equals("WarehouseEmployee"))
            {
                createWarehouseEmployee();
            }   
    }
    }
    private void createPrivateCustomer()
    {   
     System.out.println("Type Person name: ");
     String newName = reader.nextLine();
     System.out.println("Type address: ");
     String newAddress = reader.nextLine();
     System.out.println("Type phone number: ");
     int newPhoneNumber = UIInput.inputInt();    
     System.out.println("Type city: ");
     String newCity = reader.nextLine();
     System.out.println("Type ZIPCode: ");
     int newZIPCode = UIInput.inputInt();      
     System.out.println("Type maxCredit: ");
     int maxCredit = UIInput.inputInt();                           
     personController.createPrivateCustomer(newName, newAddress, newPhoneNumber, newCity, newZIPCode, maxCredit);
     back();
     System.out.println("\f");  
    }
    private void createBusinessCustomer()
    {  
     System.out.println("Type name: ");
     String newName = reader.nextLine();
     System.out.println("Type address: ");
     String newAddress = reader.nextLine();
     System.out.println("Type phone number: ");
     int newPhoneNumber = UIInput.inputInt();    
     System.out.println("Type city: ");
     String newCity = reader.nextLine();
     System.out.println("Type ZIPCode: ");
     int newZIPCode = UIInput.inputInt();       
     System.out.println("Type maxCredit: ");
     int maxCredit = UIInput.inputInt();      
     System.out.println("Type CVR Number: ");
     int newCVR = UIInput.inputInt();           
     personController.createBusinessCustomer(newName, newAddress, newPhoneNumber, newCity, newZIPCode, maxCredit, newCVR);
     back();
     System.out.println("\f");    
    }
    
    private void createSalesman()
    {   
     System.out.println("Type name: ");
     String newName = reader.nextLine();
     System.out.println("Type address: ");
     String newAddress = reader.nextLine();
     System.out.println("Type phone number: ");
     int newPhoneNumber = UIInput.inputInt();    
     System.out.println("Type city: ");
     String newCity = reader.nextLine();
     System.out.println("Type ZIPCode: ");
     int newZIPCode = UIInput.inputInt();     
     System.out.println("Type loginName: ");
     String newLoginName = reader.nextLine();     
     System.out.println("Type password: ");
     String newPassword = reader.nextLine();
     System.out.println("Type user-level: ");
     int newUserLevel = UIInput.inputInt();         
     personController.createSalesman(newName, newAddress, newPhoneNumber, newCity, newZIPCode, newLoginName,newPassword, newUserLevel);
     back();
     System.out.println("\f");    
    }       

    private void createManager()
    {     
     System.out.println("Type name: ");
     String newName = reader.nextLine();
     System.out.println("Type address: ");
     String newAddress = reader.nextLine();
     System.out.println("Type phone number: ");
     int newPhoneNumber = UIInput.inputInt();    
     System.out.println("Type city: ");
     String newCity = reader.nextLine();
     System.out.println("Type ZIPCode: ");
     int newZIPCode = UIInput.inputInt();     
     System.out.println("Type loginName: ");
     String newLoginName = reader.nextLine();   
     System.out.println("Type password: ");
     String newPassword = reader.nextLine();
     System.out.println("Type user-level: ");
     int newUserLevel = UIInput.inputInt();         
     personController.createManager(newName, newAddress, newPhoneNumber, newCity, newZIPCode, newLoginName, newPassword, newUserLevel);
     back();
     System.out.println("\f");  
  
    }   
    private void createWarehouseEmployee()
    {    
     System.out.println("Type name: ");
     String newName = reader.nextLine();
     System.out.println("Type address: ");
     String newAddress = reader.nextLine();
     System.out.println("Type phone number: ");
     int newPhoneNumber = UIInput.inputInt();    
     System.out.println("Type city: ");
     String newCity = reader.nextLine();
     System.out.println("Type ZIPCode: ");
     int newZIPCode = UIInput.inputInt();     
     System.out.println("Type loginName: ");
     String newLoginName = reader.nextLine();
     System.out.println("Type password: ");
     String newPassword = reader.nextLine();
     System.out.println("Type user-level: ");
     int newUserLevel = UIInput.inputInt();         
     personController.createWarehouseEmployee(newName, newAddress, newPhoneNumber, newCity, newZIPCode, newLoginName, newPassword, newUserLevel);
     back();
     System.out.println("\f");  
    }   
    
    
    private void deletePerson()
    {   
        
     System.out.println("Type phone number: ");
     int newPhoneNumber = UIInput.inputInt(); 
     personController.deletePerson(newPhoneNumber);
        
    }     
    
    private void updatePerson()
    {  
    System.out.println(" *** What do you want to change? *** ");
    System.out.println(" (1) Update name ");
    System.out.println(" (2) Update phonenumber ");
    System.out.println(" (3) Update address ");
    System.out.println(" (4) Update city ");    
    System.out.println(" (5) Update ZIPCode ");
    System.out.println(" (6) Update maxcredit ");
    System.out.println(" (7) Update loginName ");    
    System.out.println(" (8) Update password ");        
    System.out.println(" (9) Update user-level ");
    System.out.println(" (10) Update payment ");    
    System.out.print("\n Make your choice: ");
    int choice = UIInput.inputInt();
    selectUpdateType(choice);  
    }  
    
        
    
    private void selectUpdateType(int choice)
    {
        try
        {
            int newPhoneNumber = 0;
            switch(choice){
                case 1:         
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());
                System.out.println("Type Person name: ");
                String newName = reader.nextLine();    
                personController.updatePersonName(newPhoneNumber, newName);        
                break;
        
        
                case 2: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();   
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type NEW person id (NEW phone number)");
                int newPhone = UIInput.inputInt();  
                personController.updatePersonPhoneNumber(newPhoneNumber, newPhone);   break;
        
        
                case 3: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type person address");
                String newAddr = reader.nextLine();
                
                personController.updatePersonAddress(newPhoneNumber, newAddr);           break;
        
        
                case 4: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type person city");
                String newCity = reader.nextLine();
            
                personController.updatePersonCity(newPhoneNumber, newCity);            break;
        
        
                case 5: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type person ZIPCode");
                int newZIPCode = UIInput.inputInt();    
                personController.updatePersonZIPCode(newPhoneNumber, newZIPCode);  break;
        
        
                case 6: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type person max credit");
                int newMaxCredit = UIInput.inputInt();    
                personController.updatePersonMaxCredit(newPhoneNumber, newMaxCredit);   break;
        
        
                case 7: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type person login name");
                String newLoginName = reader.nextLine();
                personController.updatePersonLoginName(newPhoneNumber, newLoginName);   break;        
        
        
                case 8: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type person password");
                String newPassword = reader.nextLine();  
                personController.updatePersonPassword(newPhoneNumber, newPassword) ;   break;        
        
        
                case 9: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type person user level");
                int newUserLevel = UIInput.inputInt();        
                personController.updatePersonUserLevel(newPhoneNumber, newUserLevel);   break;        
        
        
                case 10: 
                System.out.println("Type person id (phone number)");
                newPhoneNumber = UIInput.inputInt();
                System.out.println("Person Selected: " + personController.findPerson(newPhoneNumber).getName());        
                System.out.println("Type person payment info");
                int newPayment = UIInput.inputInt();          
                personController.updatePersonPayment(newPhoneNumber, newPayment) ;   break;                
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        back();
    }
    
    private void findPerson()
    {
        try
        {
            System.out.println("\f");
            System.out.println("Please enter the phone number of the Person you want to find:");
            int phoneNumber = UIInput.inputInt();
            Person newPerson = personController.findPerson(phoneNumber);
            if(newPerson != null)
            {
                System.out.println("Name: "+ newPerson.getName());
                if(newPerson instanceof PrivateCustomer)
                {
                    PrivateCustomer newObj = (PrivateCustomer) newPerson;
                    System.out.println("Address: "+ newObj.getAddress());
                    System.out.println("City: "+ newObj.getCity());
                    System.out.println("Zip code: "+ newObj.getZIPCode());
                    System.out.println("Phone number: " + newObj.getPhoneNumber());
                    System.out.println("Type: " + newObj.getRoleName());
                    System.out.println("Max Credit: " + newObj.getMaxCredit());  
       
                }
                if(newPerson instanceof BusinessCustomer)
                {
                    BusinessCustomer newObj = (BusinessCustomer) newPerson;
                    System.out.println("Address: "+ newObj.getAddress());
                    System.out.println("City: "+ newObj.getCity());
                    System.out.println("Zip code: "+ newObj.getZIPCode());
                    System.out.println("Phone number: " + newObj.getPhoneNumber());
                    System.out.println("Type: " + newObj.getRoleName());      
                    System.out.println("Max Credit: " + newObj.getMaxCredit());  
                    System.out.println("CVR: " + newObj.getCVR());              
                }
                if(newPerson instanceof Salesman)
                {
                    Salesman newObj = (Salesman) newPerson;
                    System.out.println("Address: "+ newObj.getAddress());
                    System.out.println("City: "+ newObj.getCity());
                    System.out.println("Zip code: "+ newObj.getZIPCode());
                    System.out.println("Phone number: " + newObj.getPhoneNumber());
                    System.out.println("Type: " + newObj.getRoleName());    
                    System.out.println("UserLevel: " + newObj.getUserLevel());    
                    System.out.println("LoginName: " + newObj.getLoginName());    
                    System.out.println("Password: " + newObj.getPassword());                
            
                }
                if(newPerson instanceof Manager)
                {
                    Manager newObj = (Manager) newPerson;
                    System.out.println("Address: "+ newObj.getAddress());
                    System.out.println("City: "+ newObj.getCity());
                    System.out.println("Zip code: "+ newObj.getZIPCode());
                    System.out.println("Phone number: " + newObj.getPhoneNumber());
                    System.out.println("Type: " + newObj.getRoleName());              
                }        
                if(newPerson instanceof WarehouseEmployee)
                {
                    WarehouseEmployee newObj = (WarehouseEmployee) newPerson;
                    System.out.println("Address: "+ newObj.getAddress());
                    System.out.println("City: "+ newObj.getCity());
                    System.out.println("Zip code: "+ newObj.getZIPCode());
                    System.out.println("Phone number: " + newObj.getPhoneNumber());
                    System.out.println("Type: " + newObj.getRoleName());              
                }
        
            }
            else
            {
                System.out.println("No match found!");
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        back();
    }

    private void back()
    {
        System.out.println("");
        System.out.print("Press 'enter' to get back to menu");
        String i = reader.nextLine();
    }      
}

